package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.SupplyChainReportMapper;
import com.youlai.boot.system.service.SupplyChainReportService;
import com.youlai.boot.system.model.entity.SupplyChainReport;
import com.youlai.boot.system.model.form.SupplyChainReportForm;
import com.youlai.boot.system.model.query.SupplyChainReportQuery;
import com.youlai.boot.system.model.vo.SupplyChainReportVO;
import com.youlai.boot.system.converter.SupplyChainReportConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 供应链日报服务实现类
 *
 * @author xiheforever
 * @since 2025-06-01 23:18
 */
@Service
@RequiredArgsConstructor
public class SupplyChainReportServiceImpl extends ServiceImpl<SupplyChainReportMapper, SupplyChainReport> implements SupplyChainReportService {

    private final SupplyChainReportConverter supplyChainReportConverter;

    /**
     * 获取供应链日报分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<SupplyChainReportVO>} 供应链日报分页列表
     */
    @Override
    public IPage<SupplyChainReportVO> getSupplyChainReportPage(SupplyChainReportQuery queryParams) {
        Page<SupplyChainReportVO> pageVO = this.baseMapper.getSupplyChainReportPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取供应链日报表单数据
     *
     * @param id 供应链日报ID
     * @return 供应链日报表单数据
     */
    @Override
    public SupplyChainReportForm getSupplyChainReportFormData(Long id) {
        SupplyChainReport entity = this.getById(id);
        return supplyChainReportConverter.toForm(entity);
    }

    /**
     * 新增供应链日报
     *
     * @param formData 供应链日报表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveSupplyChainReport(SupplyChainReportForm formData) {
        SupplyChainReport entity = supplyChainReportConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新供应链日报
     *
     * @param id   供应链日报ID
     * @param formData 供应链日报表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateSupplyChainReport(Long id,SupplyChainReportForm formData) {
        SupplyChainReport entity = supplyChainReportConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除供应链日报
     *
     * @param ids 供应链日报ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteSupplyChainReports(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的供应链日报数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
